home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / stdio / findfp.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  5KB  |  177 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Chris Torek.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)findfp.c    5.10 (Berkeley) 2/24/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #define KERNEL
  42. #include "ixemul.h"
  43.  
  44. #include <unistd.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include "local.h"
  49. #include "glue.h"
  50.  
  51. #define NSTATIC 0
  52. #define    NDYNAMIC 10    /* add ten more whenever necessary */
  53.  
  54. void
  55. __init_stdinouterr (void)
  56. {
  57.   FILE *fp;
  58.  
  59.   fp = u.u_sF[0] = __sfp();
  60.   if (fp)
  61.     {
  62.       fp->_flags  = __SRD;
  63.       fp->_file   = STDIN_FILENO;
  64.       fp->_cookie = fp;
  65.       fp->_close  = __sclose;
  66.       fp->_read   = __sread;
  67.       fp->_seek   = __sseek;
  68.       fp->_write  = __swrite;
  69.     }
  70.  
  71.   fp = u.u_sF[1] = __sfp();
  72.   if (fp)
  73.     {
  74.       fp->_flags  = __SWR;
  75.       fp->_file   = STDOUT_FILENO;
  76.       fp->_cookie = fp;
  77.       fp->_close  = __sclose;
  78.       fp->_read   = __sread;
  79.       fp->_seek   = __sseek;
  80.       fp->_write  = __swrite;
  81.     }
  82.  
  83.   fp = u.u_sF[2] = __sfp();
  84.   if (fp)
  85.     {
  86.       fp->_flags  = __SRW|__SNBF;
  87.       fp->_file   = STDERR_FILENO;
  88.       fp->_cookie = fp;
  89.       fp->_close  = __sclose;
  90.       fp->_read   = __sread;
  91.       fp->_seek   = __sseek;
  92.       fp->_write  = __swrite;
  93.     }
  94. }
  95.  
  96. static struct glue *moreglue(int n)
  97. {
  98.     register struct glue *g;
  99.     register FILE *p;
  100.     const static FILE empty;    /* this static is ok in the library */
  101.  
  102.     g = (struct glue *)malloc(sizeof(*g) + n * sizeof(FILE));
  103.     if (g == NULL)
  104.         return (NULL);
  105.     p = (FILE *)(g + 1);
  106.     g->next = NULL;
  107.     g->niobs = n;
  108.     g->iobs = p;
  109.     while (--n >= 0)
  110.         *p++ = empty;
  111.     return (g);
  112. }
  113.  
  114. /*
  115.  * Find a free FILE for fopen et al.
  116.  */
  117. FILE *__sfp(void)
  118. {
  119.     register FILE *fp;
  120.     register int n;
  121.     register struct glue *g;
  122.  
  123.     for (g = &__sglue;; g = g->next) {
  124.         for (fp = g->iobs, n = g->niobs; --n >= 0; fp++)
  125.             if (fp->_flags == 0)
  126.                 goto found;
  127.         if (g->next == NULL && (g->next = moreglue(NDYNAMIC)) == NULL)
  128.             break;
  129.     }
  130.     return (NULL);
  131. found:
  132.     fp->_flags = 1;        /* reserve this slot; caller sets real flags */
  133.     fp->_p = NULL;        /* no current pointer */
  134.     fp->_w = 0;        /* nothing to read or write */
  135.     fp->_r = 0;
  136.     fp->_bf._base = NULL;    /* no buffer */
  137.     fp->_bf._size = 0;
  138.     fp->_lbfsize = 0;    /* not line buffered */
  139.     fp->_file = -1;        /* no file */
  140. /*    fp->_cookie = <any>; */    /* caller sets cookie, _read/_write etc */
  141.     fp->_ub._base = NULL;    /* no ungetc buffer */
  142.     fp->_ub._size = 0;
  143.     fp->_lb._base = NULL;    /* no line buffer */
  144.     fp->_lb._size = 0;
  145.     return (fp);
  146. }
  147.  
  148. /*
  149.  * XXX.  Force immediate allocation of internal memory.  Not used by stdio,
  150.  * but documented historically for certain applications.  Bad applications.
  151.  */
  152. void f_prealloc(void)
  153. {
  154.     int n = getdtablesize() - NSTATIC + 20;        /* 20 for slop */
  155.     register struct glue *g;
  156.  
  157.     for (g = &__sglue; (n -= g->niobs) > 0 && g->next; g = g->next)
  158.         /* void */;
  159.     if (n > 0)
  160.         g->next = moreglue(n);
  161. }
  162.  
  163.  
  164. /* this is put into a plain atexit() chain for stdio inside the library */
  165.  
  166. /*
  167.  * exit() calls _cleanup() through *__cleanup, set whenever we
  168.  * open or buffer a file.  This chicanery is done so that programs
  169.  * that do not use stdio need not link it all in.
  170.  *
  171.  * The name `_cleanup' is, alas, fairly well known outside stdio.
  172.  */
  173. void _cleanup(void)
  174. {
  175.   _fwalk(__sflush);        /* `cheating' */
  176. }
  177.